home *** CD-ROM | disk | FTP | other *** search
/ Nejlepší hry / Nejlepsi hry.iso / hry / plane arcade / planearcade.exe / tank3.bmp / engine.cpp < prev    next >
C/C++ Source or Header  |  2004-09-28  |  16KB  |  554 lines

  1. #include "main.h"
  2.  
  3. //---------------------------------------------------------------------
  4. //Globalne PremennΘ
  5. //---------------------------------------------------------------------
  6.  
  7. //DIRETCX
  8. LPDIRECT3D9             g_pD3D       = NULL; 
  9. LPDIRECT3DDEVICE9       g_pd3dDevice = NULL;
  10.  
  11. //WINDOWS
  12. HWND      hWnd = NULL;
  13. HINSTANCE hIns = NULL;
  14.  
  15. //MATICA
  16. D3DXMATRIXA16 matProj;   //projekcia
  17.  
  18. //ENGINE
  19. ENGINE Engine;
  20.  
  21. /*
  22. //--------------------------------------------------------------------
  23. // Name: Initialze()
  24. // Desc: Pred Inicializaciou musia byt zadane Height, Width a Windowed
  25. //--------------------------------------------------------------------
  26. */
  27. void ENGINE::Initialize()
  28. {
  29.  
  30.     //log start
  31.     char cBuf[80];
  32.     LogPrint(">>>Inicializijem ENGINE");
  33.     LogPrint("Nastavenia:");
  34.  
  35.     sprintf(cBuf,"   Sirka: %d  Vyska: %d",Width,Height);
  36.     LogPrint(cBuf);
  37.  
  38.     sprintf(cBuf,"   Windowed: %d ",Windowed);
  39.     LogPrint(cBuf);
  40.  
  41.     sprintf(cBuf,"   Max: %.2f  Min: %.2f",Max,Min);
  42.     LogPrint(cBuf);
  43.  
  44.     sprintf(cBuf,"   BackColor: %f %f %f",BackColor.R,BackColor.G,BackColor.B);
  45.     LogPrint(cBuf);
  46.  
  47.     sprintf(cBuf,"   Format Obrazovky: %d ",Format);
  48.     LogPrint(cBuf);
  49.  
  50.     sprintf(cBuf,"   Format Textur: %d ",TextureFormat);
  51.     LogPrint(cBuf);
  52.  
  53.     sprintf(cBuf,"   Stencil Shadows: %d ",StencilShadows);
  54.     LogPrint(cBuf);
  55.     
  56.  
  57.     /////////////////////
  58.     //Vytvorenie g_pD3D//
  59.     /////////////////////
  60.     if (FAILED(g_pD3D = Direct3DCreate9( D3D_SDK_VERSION )))
  61.     {
  62.         LogPrint("Nemozno vytvorit g_pD3D");
  63.     }
  64.     else
  65.     {
  66.         LogPrint("g_pD3D vytvorene");
  67.     }
  68.    
  69.  
  70.     //zisti mod obrazovky
  71.     D3DDISPLAYMODE d3ddm;
  72.     g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
  73.  
  74.     ////////////////////////
  75.      //nastavanie Viewportu//
  76.     ////////////////////////
  77.     Viewport.Height = Height;
  78.     Viewport.Width = Width;
  79.     Viewport.X = 0;
  80.     Viewport.Y = 0;
  81.     Viewport.MaxZ = Max;
  82.     Viewport.MinZ = Min;
  83.  
  84.     ////////////////////
  85.     //paremetre obrazu//
  86.     ////////////////////
  87.     D3DPRESENT_PARAMETERS d3dpp; 
  88.  
  89.     ZeroMemory( &d3dpp, sizeof(d3dpp) );
  90.  
  91.     d3dpp.Windowed = Windowed;
  92.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  93.  
  94.     d3dpp.BackBufferWidth   = Width;
  95.     d3dpp.BackBufferHeight  = Height ;
  96.     
  97.     // !! nastavi 60 hz pre moj pitomi monitor !!
  98.     //if (Resolution == 2) d3dpp.FullScreen_RefreshRateInHz = 60;
  99.  
  100.     if (Windowed == true)
  101.         d3dpp.BackBufferFormat = d3ddm.Format ;
  102.     else
  103.         d3dpp.BackBufferFormat  = Format;
  104.  
  105.     d3dpp.EnableAutoDepthStencil = TRUE;
  106.     d3dpp.Flags                  = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;
  107.     d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;
  108.     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  109.  
  110.     ////////////////////////////////
  111.     //Zisti podporu Stencil Shadow//
  112.     ////////////////////////////////
  113.     if (StencilShadows == true)
  114.     {
  115.         d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
  116.  
  117.         if(FAILED(g_pD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
  118.             d3dpp.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL, 
  119.             D3DRTYPE_SURFACE, D3DFMT_D24S8)))
  120.         {
  121.             
  122.             StencilShadows = false;
  123.             LogPrint("Vasa karta nepodporuje Stencil Buffer");
  124.         }
  125.         else
  126.         {
  127.             if(FAILED(g_pD3D->CheckDepthStencilMatch(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
  128.                 d3dpp.BackBufferFormat, d3dpp.BackBufferFormat, D3DFMT_D24S8)))
  129.             {
  130.                 StencilShadows = false;
  131.                 LogPrint("Depht - Stencil nieje kompatibliny s modom obrazovky");
  132.             }
  133.         }
  134.  
  135.         //preni mod ak je stencil vypnuty
  136.         if (StencilShadows == false)
  137.             d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  138.  
  139.     }
  140.  
  141.     //////////////////////////
  142.     //inicializacia Direct3D//
  143.     //////////////////////////
  144.     if (FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  145.                                      D3DCREATE_HARDWARE_VERTEXPROCESSING,
  146.                                      &d3dpp, &g_pd3dDevice ))) 
  147.     {
  148.         if (FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  149.                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  150.                                      &d3dpp, &g_pd3dDevice ))) 
  151.         {
  152.             LogPrint("chyba pri inicializacii g_pd3dDevice ") ;
  153.         }
  154.     }
  155.     else
  156.     {
  157.         LogPrint("inicializacia g_pd3dDevice  uspesna") ;
  158.     }
  159.  
  160.  
  161.     ///////////
  162.     //D3DCAPS//
  163.     ///////////
  164.     D3DCAPS9 Caps;
  165.     g_pd3dDevice->GetDeviceCaps(&Caps);
  166.  
  167.     //stencil
  168.     if (StencilShadows == true)
  169.     {
  170.         if (!(Caps.StencilCaps & D3DSTENCILCAPS_DECR) ||
  171.             !(Caps.StencilCaps & D3DSTENCILCAPS_INCR) ||
  172.             !(Caps.StencilCaps & D3DSTENCILCAPS_KEEP))
  173.         {
  174.             LogPrint("D3D Device nemoze pouzit Stencil Buffer");
  175.             StencilShadows = false;
  176.         }
  177.     }
  178.  
  179.     //////////////////////
  180.     //OSTATNE NASTAVENIA//
  181.     //////////////////////
  182.  
  183.     //normalizuj normaly
  184.     g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS,TRUE);
  185.  
  186.     //deepht bias slope scale
  187.     g_pd3dDevice->SetRenderState( D3DRS_SLOPESCALEDEPTHBIAS, FtoDW(-0.3f) );
  188.     g_pd3dDevice->SetRenderState( D3DRS_DEPTHBIAS, FtoDW(0.0f) );
  189.  
  190.     //ambient
  191.     g_pd3dDevice->SetRenderState( D3DRS_AMBIENT,0xff555555);
  192.   
  193. }
  194.  
  195. /*
  196. //--------------------------------------------------------------------
  197. // Name: CleanUp()
  198. // Desc: Vycistenie pamati
  199. //--------------------------------------------------------------------
  200. */
  201. void ENGINE::CleanUp()
  202. {
  203.     if( g_pd3dDevice != NULL ) 
  204.         g_pd3dDevice->Release();
  205.  
  206.     if( g_pD3D != NULL )       
  207.         g_pD3D->Release();
  208. }
  209.  
  210.  
  211. /*
  212. //--------------------------------------------------------------------
  213. // Name: SetBlendNone()
  214. // Desc: Ziadna priesvitnost - vypne priesvitnos¥
  215. //--------------------------------------------------------------------
  216. */
  217. void ENGINE::SetBlendNone()
  218. {
  219.     g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,false);
  220.     g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ZERO );  
  221.     g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ZERO);
  222. }
  223.  
  224. /*
  225. //--------------------------------------------------------------------
  226. // Name: SetBlendOne()
  227. // Desc: Jednoducha priesvitnost
  228. //--------------------------------------------------------------------
  229. */
  230. void ENGINE::SetBlendOne()
  231. {
  232.     g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
  233.     g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_ONE );  
  234.     g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
  235. }
  236.  
  237. /*
  238. //--------------------------------------------------------------------
  239. // Name: SetBlendTrans()
  240. // Desc: Jedna farba bude transparentna, zalezi na texture aka
  241. //--------------------------------------------------------------------
  242. */
  243. void ENGINE::SetBlendTrans()
  244. {
  245.     g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
  246.     g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA );  
  247.     g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
  248.  
  249. }
  250.  
  251. /*
  252. //--------------------------------------------------------------------
  253. // Name: SetBlendCustom()
  254. // Desc: Umo₧nuje si nastavi¥ vlastnu transparentnos¥ podla D3D
  255. //--------------------------------------------------------------------
  256. D3D TYPY ktore mozno pouzit
  257.     D3DBLEND_ZERO               = 1,
  258.     D3DBLEND_ONE                = 2,
  259.     D3DBLEND_SRCCOLOR           = 3,
  260.     D3DBLEND_INVSRCCOLOR        = 4,
  261.     D3DBLEND_SRCALPHA           = 5,
  262.     D3DBLEND_INVSRCALPHA        = 6,
  263.     D3DBLEND_DESTALPHA          = 7,
  264.     D3DBLEND_INVDESTALPHA       = 8,
  265.     D3DBLEND_DESTCOLOR          = 9,
  266.     D3DBLEND_INVDESTCOLOR       = 10,
  267.     D3DBLEND_SRCALPHASAT        = 11,
  268.     D3DBLEND_BOTHSRCALPHA       = 12,
  269.     D3DBLEND_BOTHINVSRCALPHA    = 13,
  270. */
  271.  
  272. void ENGINE::SetBlendCustom(_D3DBLEND Scr,_D3DBLEND Dest)
  273. {
  274.     g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
  275.     g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,Scr );  
  276.     g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,Dest);
  277. }
  278.  
  279. /*
  280. //--------------------------------------------------------------------
  281. // Name: SetCullMode()
  282. // Desc: Nastavi orezavanie (prednΘ,zadne, ziadne)
  283. //--------------------------------------------------------------------
  284.     D3DCULL_NONE                = 1,
  285.     D3DCULL_CW                  = 2,
  286.     D3DCULL_CCW                 = 3,
  287. */
  288. void ENGINE::SetCullMode(_D3DCULL Cull)
  289. {
  290.     g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, Cull );
  291. }
  292.  
  293. /*
  294. //--------------------------------------------------------------------
  295. // Name: SetFillMode()
  296. // Desc: Nastavi mod naplnovania
  297. //--------------------------------------------------------------------
  298.     D3DFILL_POINT               = 1,
  299.     D3DFILL_WIREFRAME           = 2,
  300.     D3DFILL_SOLID               = 3,
  301. */
  302. void ENGINE::SetFillMode(_D3DFILLMODE Fill)
  303. {
  304.     g_pd3dDevice->SetRenderState(D3DRS_FILLMODE ,Fill);
  305. }
  306.  
  307. //--------------------------------------------------------------------
  308. // Name: SetFilter()
  309. // Desc: Nastavi filtrovanie textur
  310. //--------------------------------------------------------------------
  311. /*  D3DTEXF_NONE            = 0,    // filtering disabled (valid for mip filter only)
  312.     D3DTEXF_POINT           = 1,    // nearest
  313.     D3DTEXF_LINEAR          = 2,    // linear interpolation
  314.     D3DTEXF_ANISOTROPIC     = 3,    // anisotropic
  315.     D3DTEXF_PYRAMIDALQUAD   = 6,    // 4-sample tent
  316.     D3DTEXF_GAUSSIANQUAD    = 7,    // 4-sample gaussian
  317. */
  318.  
  319. void ENGINE::SetFilter(int Stage, _D3DTEXTUREFILTERTYPE Filter)
  320. {
  321.     g_pd3dDevice->SetSamplerState(Stage, D3DSAMP_MINFILTER, Filter);
  322.     g_pd3dDevice->SetSamplerState(Stage, D3DSAMP_MAGFILTER, Filter);
  323. }
  324.  
  325. /*
  326. //--------------------------------------------------------------------
  327. // Name: SetShadeMode()
  328. // Desc: Nastavi shading polygonov
  329. //--------------------------------------------------------------------
  330.     D3DSHADE_FLAT               = 1,
  331.     D3DSHADE_GOURAUD            = 2,
  332.     D3DSHADE_PHONG              = 3,
  333. */
  334. void ENGINE::SetShadeMode(_D3DSHADEMODE Shade)
  335. {
  336.     g_pd3dDevice->SetRenderState(D3DRS_SHADEMODE ,Shade);
  337. }
  338.  
  339. /*
  340. //--------------------------------------------------------------------
  341. // Name: SetZBuffer()
  342. // Desc: zapne - vypne ZBuffer
  343. //--------------------------------------------------------------------
  344. */
  345. void ENGINE::SetZBuffer(bool Activate)
  346. {
  347.     g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, Activate);
  348. }
  349.  
  350. /*
  351. //--------------------------------------------------------------------
  352. // Name: SetMipMapping()
  353. // Desc: zapne - vypne MipMapping, Levels - kolko mipmaplevelov vytvara¥
  354. //--------------------------------------------------------------------
  355. */
  356. void ENGINE::SetMipMapping(int Stage,bool Activate) 
  357. {
  358.     if (Activate == true) 
  359.     {
  360.         g_pd3dDevice->SetSamplerState(Stage, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
  361.     }
  362.     else
  363.     {
  364.         g_pd3dDevice->SetSamplerState(Stage, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
  365.     }
  366.  
  367.         
  368. }
  369.  
  370. /*
  371. //--------------------------------------------------------------------
  372. // Name: SetProjection
  373. // Desc: nastavi projekcnu maticu
  374. //--------------------------------------------------------------------
  375. */
  376. void ENGINE::SetProjection()
  377. {
  378.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/3.6f, 1.0f,Min, Max);
  379.     g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  380. }
  381.  
  382. /*
  383. //--------------------------------------------------------------------
  384. // Name: SetLighting()
  385. // Desc: Vypne zapne osvetlenie
  386. //--------------------------------------------------------------------
  387. */
  388. void ENGINE::SetLighting(bool Activate)
  389. {
  390.     g_pd3dDevice->SetRenderState( D3DRS_LIGHTING,Activate);
  391. }
  392.  
  393. /*
  394. //--------------------------------------------------------------------
  395. // Name: SetFog()nastavi hmlu
  396. // Desc: Vypne zapne osvetlenie
  397. //--------------------------------------------------------------------
  398. */
  399. void ENGINE::SetupFog(COLOR Color, float Destiny) 
  400. {    
  401.     g_pd3dDevice->SetRenderState(D3DRS_FOGCOLOR, D3DXCOLOR(Color.R,Color.G,Color.B,Color.A ));
  402.     g_pd3dDevice->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_EXP2);
  403.     g_pd3dDevice->SetRenderState(D3DRS_FOGDENSITY, FtoDW(Destiny));
  404.        
  405. }
  406.  
  407. /*
  408. //--------------------------------------------------------------------
  409. // Name: SetFogActive(()
  410. // Desc: Vypne zapne hmli
  411. //--------------------------------------------------------------------
  412. */
  413. void ENGINE::SetFog(bool Activate)
  414. {
  415.     g_pd3dDevice->SetRenderState(D3DRS_FOGENABLE, Activate);
  416. }
  417.  
  418. /*
  419. //--------------------------------------------------------------------
  420. // Name: SetSpecular()
  421. // Desc: Vypne zapne specular svetla
  422. //--------------------------------------------------------------------
  423. */
  424. void ENGINE::SetSpecular(bool Activate)
  425. {
  426.     g_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE,Activate);
  427. }
  428.  
  429. /*
  430. //--------------------------------------------------------------------
  431. // Name: SetAlphaTest()
  432. // Desc: Vypne alpha test, umo₧ni kresli¥ transparentne objekty nakoniec
  433. //--------------------------------------------------------------------
  434. */
  435. void ENGINE::SetAlphaTest(bool Activate)
  436. {
  437.     g_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE,Activate);
  438.     g_pd3dDevice->SetRenderState(D3DRS_ALPHAREF, Activate);
  439.     g_pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
  440. }
  441.  
  442.  
  443. /*
  444. //--------------------------------------------------------------------
  445. // Name: SetZwrite()
  446. // Desc: Vypne alebo zapne zapisovanie do hlbkoveho bufferu
  447. //--------------------------------------------------------------------
  448. */
  449. void ENGINE::SetZwrite(bool Activate)
  450. {
  451.     g_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE,Activate);
  452. }
  453.  
  454. /*
  455. //--------------------------------------------------------------------
  456. // Name: SetStencil()
  457. // Desc: Vypne alebo zapne stencil
  458. //--------------------------------------------------------------------
  459. */
  460. void ENGINE::SetStencil(bool Activate)
  461. {
  462.     g_pd3dDevice->SetRenderState( D3DRS_STENCILENABLE,Activate );
  463. }
  464.  
  465.  
  466. /*
  467. //--------------------------------------------------------------------
  468. // Name: SetSpecular()
  469. // Desc: zresetuje nastavenia svetiel a priesvitnosti
  470. //--------------------------------------------------------------------
  471. */
  472.  
  473. void ENGINE::ResetToDefault()
  474. {
  475.     
  476.     Engine.SetLighting(false);
  477.     Engine.SetSpecular(false);
  478.     Engine.SetZBuffer(true);
  479.     Engine.SetZwrite(true);
  480.     Engine.SetStencil(false);
  481.     Engine.SetAlphaTest(false);
  482.  
  483.     Engine.SetCullMode(D3DCULL_NONE);
  484.     Engine.SetShadeMode(D3DSHADE_GOURAUD);
  485.  
  486.      g_pd3dDevice->SetTextureStageState(0 ,D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  487.     g_pd3dDevice->SetTextureStageState(0 ,D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  488.     g_pd3dDevice->SetTextureStageState(0 ,D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  489.  
  490.     g_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX,0);
  491.     g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
  492.     g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  493.     g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  494.  
  495.     g_pd3dDevice->SetTextureStageState(1 ,D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  496.     g_pd3dDevice->SetTextureStageState(1 ,D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  497.     g_pd3dDevice->SetTextureStageState(1 ,D3DTSS_ALPHAARG2, D3DTA_CURRENT);
  498.  
  499.     g_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX,1);
  500.     g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
  501.     g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  502.     g_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
  503.  
  504. }
  505.  
  506. /*
  507. //--------------------------------------------------------------------
  508. // Name: BeginScene()
  509. // Desc: zacni vykreslovanie
  510. //--------------------------------------------------------------------
  511. */
  512. void ENGINE::BeginScene()
  513. {
  514.  
  515.     //zacniFPS
  516.     TimeStart();
  517.  
  518.     //vytvori flagy pre vycistenie
  519.     DWORD flags = 0;
  520.     flags |= D3DCLEAR_TARGET;
  521.     flags |= D3DCLEAR_ZBUFFER;
  522.  
  523.     if (StencilShadows)
  524.         flags |= D3DCLEAR_STENCIL;
  525.  
  526.     //Vicistenie backbufferu
  527.     g_pd3dDevice->Clear( 0, NULL, flags, D3DXCOLOR(Engine.BackColor.R,Engine.BackColor.G,Engine.BackColor.B,1.0f), 1.0f, 0 );
  528.     
  529.     //zacni scenu
  530.     g_pd3dDevice->BeginScene();
  531.  
  532. }
  533.  
  534. /*
  535. //--------------------------------------------------------------------
  536. // Name: EndScene()
  537. // Desc: prezentuj scenu
  538. //--------------------------------------------------------------------
  539. */
  540. void ENGINE::EndScene()
  541. {
  542.  
  543.     //ukonci scenu    
  544.     g_pd3dDevice->EndScene();
  545.  
  546.     //Zbraz na Obrazovku
  547.     g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
  548.  
  549.     //skonciFPS
  550.     TimeEnd();
  551.  
  552. }
  553.  
  554.